package com.burgeon.backend.EnterpriseWeChat.rest.vip;

import com.burgeon.backend.EnterpriseWeChat.entity.Vip;
import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class FindVipNewstByCardNo extends RESTBase {
    private final static String RANGE = "20";//分页数量
    private String ip;
    private String account;
    private String password;

    public FindVipNewstByCardNo(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public Vip getData(String cardNo,String empId,String storeId,String creatIonDate,String endTime) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(cardNo,empId,storeId,creatIonDate,endTime), url);
        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(new JSONArray(str).getJSONObject(0).getString("message"));
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
//        ArrayList<Vip> vipList = new ArrayList<>();
//        System.err.println(jsonArray.length());

        if(jsonArray.length()>0){
            Vip vip = new Vip();
                vip.setVipName(jsonArray.getJSONArray(0).getString(0));
                vip.setCardNo(jsonArray.getJSONArray(0).getString(1));
                vip.setLogoImg(jsonArray.getJSONArray(0).getString(2));
                vip.setOpenid(jsonArray.getJSONArray(0).getString(3));
                vip.setAmount(jsonArray.getJSONArray(0).getString(4));
                vip.setIntegral(jsonArray.getJSONArray(0).getString(5));
                vip.setId(jsonArray.getJSONArray(0).getString(6));
                vip.setTotAmtActual(jsonArray.getJSONArray(0).getString(7));
                vip.setLastDate(jsonArray.getJSONArray(0).getString(8));
                vip.setLastAmtActual(jsonArray.getJSONArray(0).getString(9));
                vip.setPhone(jsonArray.getJSONArray(0).getString(10));
            return vip;
        }

        return null;
    }

    private MultiValueMap Data(String cardNo,String empId,String storeId,String creatIonDate,String endTime) {
        HashMap params = new HashMap();
        ArrayList colums = new ArrayList();//查询字段

        colums.add("VIPNAME");//vip姓名
        colums.add("CARDNO");//卡号
        colums.add("LOGO_URL");//头像
//        colums.add("YC_8LABELDEFINE_ID");//累计购物金额
        colums.add("OPENID2");
        colums.add("AMOUNT");//储值余额
        colums.add("INTEGRAL");//积分
        colums.add("ID");
        colums.add("'FA_VIPACC_ID;TOT_AMT_ACTUAL'");//	累计购物金额
        colums.add("'FA_VIPACC_ID;LASTDATE'");//最近购物日
        colums.add("'FA_VIPACC_ID;LAST_AMT_ACTUAL'");//最近购物金额
        colums.add("MOBIL");

        params.put("columns", colums);
        params.put("table", "C_VIP");//表名

        params.put("count","true");

        HashMap paramss = new HashMap();
        HashMap expr1 = new HashMap();
        expr1.put("column", "'CARDNO'");
        expr1.put("condition", cardNo);
        HashMap expr2 = new HashMap();
        if(empId != null && !empId.isEmpty()){

            expr2.put("column", "'HR_EMPLOYEE_ID;ID'");
            expr2.put("condition", empId);
        }else {
            expr2.put("column", "'C_STORE_ID'");
            expr2.put("condition", storeId);
        }
        HashMap expr4 = new HashMap();
        expr4.put("combine", "and");
        expr4.put("expr1", expr1);
        expr4.put("expr2", expr2);

        HashMap expr3 = new HashMap();
        String ac =  "'"+creatIonDate+"~"+endTime+"'";
        expr3.put("column", "'CREATIONDATE'");
        expr3.put("condition", ac);

        paramss.put("combine", "and");
        paramss.put("expr1", expr4);
        paramss.put("expr2", expr3);

        params.put("params", paramss);

        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        FindVipNewstByCardNo g = new FindVipNewstByCardNo("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            String empId = null;
            System.err.println(g.getData("13643111382","","3128","20201201","20201230"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
